using SS;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SM.core
{
	internal class National
	{
		private Func objFunc = new Func();
		private string strConnection = "";
		public National()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public int action(string ID, string Name, string Des)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabNational", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Name", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("Des", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["Name"].Value = Name;
				sqlCommand.Parameters["Des"].Value = Des;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public void loadGridView(DataGridView dgv, BindingNavigator bdn)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string selectCommandText = "SELECT ID, Name, Des FROM [TabNational] ORDER BY [Name]";
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, sqlConnection);
			DataSet dataSet = new DataSet();
			sqlDataAdapter.Fill(dataSet);
			BindingSource bindingSource = new BindingSource();
			bindingSource.DataSource = dataSet.Tables[0];
			bdn.BindingSource = bindingSource;
			dgv.DataSource = bindingSource;
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public string getNational(string ID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = "SELECT Name FROM [TabNational] WHERE ID = '" + ID + "'";
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = sqlDataReader["Name"].ToString();
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public static void getListNational()
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string commandText = "SELECT * FROM [TabNational]";
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = commandText;
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					Program.listNational.Add(sqlDataReader["ID"].ToString(), sqlDataReader["Name"].ToString());
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		public static void getListProvider()
		{
			try
			{
				Program.listProvider.Clear();
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string commandText = "SELECT CusID, Name FROM [TabPartner] WHERE [IsProvider] = 1";
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = commandText;
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					Program.listProvider.Add(sqlDataReader["CusID"].ToString(), sqlDataReader["Name"].ToString());
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		public int delItem(string ID)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string cmdText = "DELETE [TabNational] WHERE [ID] = '" + ID + "'";
				SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public string CheckEmpExitstInOutBill(string tmpAutoID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			try
			{
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT [ID] FROM [TabInOutBill] WHERE [NationalID] = '" + tmpAutoID + "'";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				if (sqlDataReader.HasRows)
				{
					result = "1";
				}
				sqlDataReader.Close();
				sqlDataReader.Dispose();
			}
			catch
			{
			}
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
	}
}
